﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CPETwit.Data.Entity;
using CPETwit.Web.Service.Interface;
using CPETwit.Web.Service.Implementation;

namespace CPETwit.Web.Pages
{
    public partial class Following : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.isLoggedViaSession();
        }

        /// <summary>
        /// Test si l'internaute est connecté ;
        /// Si ce n'est pas le cas, on le redirige vers la page d'accueil
        /// </summary>
        private void isLoggedViaSession()
        {
            bool isConnected = false;
            foreach (var item in Session.Keys)
            {
                if (String.Equals(item, "login"))
                {
                    isConnected = true;
                    /* Transmission du sexe de l'utilisateur */
                    String sexe = ((User)Session["login"]).sex;
                    Session.Add("sexe", sexe);
                    break;
                }
            }
            if (!isConnected)
            {
                Response.Redirect("connexion.aspx");
            }
        }

        /// <summary>
        /// S'abonner à un flux.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void FollowLinkButton_Click(object sender, EventArgs e)
        {
            /* On récupère l'utilisateur cliqué dans la liste par CommandeArgument */
            LinkButton lb = (LinkButton)sender;
            if (lb.CommandArgument != null)
            {
                ITwitterService tw = new TwitterService();
                User follower = (User)Session["login"];
                User publisher = tw.GetUser(lb.CommandArgument);
                tw.Follow(follower, publisher);
            }
        }

        /// <summary>
        /// Se désabonner d'un flux
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void UnfollowLinkButton_Click(object sender, EventArgs e)
        {
            LinkButton lb = (LinkButton)sender;
            if (lb.CommandArgument != null)
            {
                ITwitterService tw = new TwitterService();
                User follower = (User)Session["login"];
                User publisher = tw.GetUser(lb.CommandArgument);
                tw.Unfollow(follower, publisher);
            }

        }

        /// <summary>
        /// Opération sur création d'un item dans un objet ListView.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ListViewPublishers_ic(object sender, ListViewItemEventArgs e)
        {
            /*ListViewItem currentItem = e.Item;
            User currentPublisher = (User)currentItem.DataItem;
            ITwitterService tw = new TwitterService();

            if (currentPublisher != null)
            {
                foreach (User item in tw.GetPublishers((User)Session["login"]))
                {
                    if (item.login.Equals(currentPublisher.login))
                    {
                        break;
                    }
                }
            }*/
        }
    }
}